<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"inkgn.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="For循环中的onclick事件这个问题是我在写一个小例子的时候遇到的，对于前端初学者来说可能是个大坑！书中其实也有讲过但是并不全面。">
<meta property="og:type" content="article">
<meta property="og:title" content="JS学习笔记（二）">
<meta property="og:url" content="https://inkgn.gitee.io/2018/11/23/newclass2/index.html">
<meta property="og:site_name" content="静水深流">
<meta property="og:description" content="For循环中的onclick事件这个问题是我在写一个小例子的时候遇到的，对于前端初学者来说可能是个大坑！书中其实也有讲过但是并不全面。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(8).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(9).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(10).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(11).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(12).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(13).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(1).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(2).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(3).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(4).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(5).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(6).png">
<meta property="og:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(7).png">
<meta property="article:published_time" content="2018-11-23T05:04:45.000Z">
<meta property="article:modified_time" content="2018-11-23T05:04:45.000Z">
<meta property="article:author" content="yinks">
<meta property="article:tag" content="JS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(8).png">

<link rel="canonical" href="https://inkgn.gitee.io/2018/11/23/newclass2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>JS学习笔记（二） | 静水深流</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">静水深流</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/categories" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-motto">

    <a href="/motto/motto" rel="section"><i class="fa fa-book fa-fw"></i>古训</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/me" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://inkgn.gitee.io/2018/11/23/newclass2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
      <meta itemprop="name" content="yinks">
      <meta itemprop="description" content="长路漫漫，半程风雨，半程春">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="静水深流">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS学习笔记（二）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-11-23 13:04:45" itemprop="dateCreated datePublished" datetime="2018-11-23T13:04:45+08:00">2018-11-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JS/" itemprop="url" rel="index"><span itemprop="name">JS</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="For循环中的onclick事件"><a href="#For循环中的onclick事件" class="headerlink" title="For循环中的onclick事件"></a>For循环中的onclick事件</h4><p>这个问题是我在写一个小例子的时候遇到的，对于前端初学者来说可能是个大坑！书中其实也有讲过但是并不全面。</p>
<p>好。我们先看案例：</p>
<p>我在页面设置五个按钮，用js实现点击按钮1，alert弹出0；并且依次增加，For循环遍历每一个按钮oBtn;按钮后添加点击事件，点击按钮后执行一个匿名函数；</p>
<p>我们理想中：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">oBtn[<span class="number">0</span>].onclick=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; alert(<span class="number">0</span>); &#125;</span><br><span class="line">oBtn[<span class="number">1</span>].onclick=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; alert(<span class="number">1</span>); &#125;</span><br><span class="line">oBtn[<span class="number">2</span>].onclick=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; alert(<span class="number">2</span>); &#125;</span><br><span class="line">oBtn[<span class="number">3</span>].onclick=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; alert(<span class="number">3</span>); &#125;</span><br><span class="line">oBtn[<span class="number">4</span>].onclick=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; alert(<span class="number">4</span>); &#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(8).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(9).png" alt="js"></p>
<p>But,往往我们这么写弹出的内容是这样：</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(10).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(11).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(12).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(13).png" alt="js"></p>
<p>这种结果往往令我们初学者困惑不已，因为不管是代码上还是在控制台都是没有错误的。那么为什么会出现这种bug呢？</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(1).png" alt="js"></p>
<p>其实问题就出现在我们的函数上，一般函数有一个很重要的特点就是如果你不调用它，它永远不会自动执行（自调函数除外），但是循环会在页面加载完成之后迅速执行，<br>那么我们的代码会是这样的：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">      <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;oBtn.length; i++)&#123;</span><br><span class="line"></span><br><span class="line">          Alert(i);   <span class="comment">// 为了试验这里加一个弹框！</span></span><br><span class="line"></span><br><span class="line">              oBtn[i].onclick=  <span class="comment">/*function()&#123;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">                  alert(i);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">              &#125;*/</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>那么弹框中会分别输出 0， 1， 2， 3， 4，此时for循环结束，当我们点击按钮调用函数时，函数中的i会默认选择离自己较近的参数所以值为循环最后5；（为什么不是4？建议好好了解下for 。涉及到浏览器解析机制我们下回讲解）</p>
<p>解决它的方法也很多，至少目前学完原生js你应该以下具备三种解决方案</p>
<h5 id="解决方案1："><a href="#解决方案1：" class="headerlink" title="解决方案1："></a>解决方案1：</h5><p><strong>利用闭包函数</strong></p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(2).png" alt="js"></p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(3).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(4).png" alt="js"><br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(5).png" alt="js">       </p>
<p>好，从上图可以看到我们的输出结果全部显示正常，接下来就让我们看看执行过程</p>
<p>我们先把代码粘贴过来：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;oBtn.length; i++)&#123;</span><br><span class="line"></span><br><span class="line">    oBtn[i].onclick=(<span class="function"><span class="keyword">function</span>(<span class="params">i</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;alert(i);&#125;</span><br><span class="line"></span><br><span class="line">    &#125;)(i);</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>首先呢，我们看一下，这次代码与上次不同之处就是在原来的函数之外增加了一个<br>自调函数，那么也就说明for循环的每一遍这个自调函数都会执行（再次涉及到作用域）<br>那么自调函数每次循环都会传递一个参数i;从而每个i都被传输到函数内（函数即作用域）<br>这样内部返回的函数就能直接从父级作用域找到参数i;（这种现象叫作用域链）</p>
<h5 id="案例2："><a href="#案例2：" class="headerlink" title="案例2："></a>案例2：</h5><p>第二种方法原理和第一种相同，只是看起来更‘正常’一些吧。</p>
<p><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(6).png" alt="js"><br>需要注意的是onclick点击以后是调用函数所以你可千万不能写bt(i)();<br>具体原理我也不多说了和上一个差不多的；<br><img src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/2021/JS%E8%B8%A9%E5%9D%91%E7%AC%94%E8%AE%B0%EF%BC%88%E4%BA%8C%EF%BC%89/%20(7).png" alt="js"></p>
<p>好，下面我们来看第三种方法：</p>
<p>给元素添加自定义属性</p>
<p>Ok! 话不多说，上代码！！！</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;oBtn.length; i++)&#123;</span><br><span class="line">            </span><br><span class="line">    oBtn[i].index=i;</span><br><span class="line"></span><br><span class="line">    oBtn[i].onclick=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">        alert(<span class="built_in">this</span>.index);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个其实就比较好理解了，我们通过for给每一个btn都加一个自定义属性并且属性是和i；同步的，也就是说btn1=0;btn2=1;btn3=2;btn4=3;btn5=4;  ok!  这样我们利用this指向当前的btn，并且输出当前btn下是index属性。</p>
<p>好，到这里我们的三种方法就说完了，源代码我会传到群文件内，有兴趣的可以下载研究一下，今天说的这些对于初学者半浅不深，虽说涉及到闭包，作用域以及浏览器解析原理，但并不深入，至于这些具体内容我们以后再探讨！！！</p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>yinks
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://inkgn.gitee.io/2018/11/23/newclass2/" title="JS学习笔记（二）">https://inkgn.gitee.io/2018/11/23/newclass2/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JS/" rel="tag"># JS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2018/11/08/class/" rel="prev" title="自学方法论">
      <i class="fa fa-chevron-left"></i> 自学方法论
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/04/02/git/" rel="next" title="git 学习笔记（一）">
      git 学习笔记（一） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#For%E5%BE%AA%E7%8E%AF%E4%B8%AD%E7%9A%84onclick%E4%BA%8B%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">For循环中的onclick事件</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%881%EF%BC%9A"><span class="nav-number">1.1.</span> <span class="nav-text">解决方案1：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B2%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">案例2：</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yinks"
      src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
  <p class="site-author-name" itemprop="name">yinks</p>
  <div class="site-description" itemprop="description">长路漫漫，半程风雨，半程春</div>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/inkgn" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;inkgn" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/2087104694@qq.com" title="E-Mail → 2087104694@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yinks</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
